package com.webapp.dao.els;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;

import com.bridge.domain.AbstractVO;
import com.els.ElsConnection;
import com.els.domain.QueryRespVO;
import com.els.factory.ElasticFactory;
import com.google.gson.JsonObject;
import com.webapp.dao.AbstractDAO;
import com.webapp.dao.els.ElsIndexInterface;

public abstract class ElsAbstractDAO implements ECHandler{
	protected Logger logger=Logger.getLogger(this.getClass());
	protected String indexName="commandconfig";
	protected String typeName;
	protected ElsConnection ec;
	protected ElsIndexInterface proxy;
	/**
	 * 进行els查询前的操作，由代理类来完成
	 */
	public void initialEc(){
		ec=ElasticFactory.getConnection();
		logger.debug("initialEc success!");
	}
	/**
	 * 进行els查询后的操作，由代理类来完成
	 */
	public void closeEc(){
		logger.info("refresh "+indexName);
		ec.getClient().admin().indices().refresh(new RefreshRequest(indexName));	//刷新索引
		logger.debug("closeEc success!");
	}
	public <T> String addOne(T one) {
		String _id=ec.insertOne(indexName, typeName, null, AbstractVO.toJsonObject(one));
		logger.debug("insert success! id :"+_id);
		return _id;
	}
	public <T> boolean deleteOne(T one){
		String _id=AbstractVO.toJsonObject(one).get("_id")==null?
				"":AbstractVO.toJsonObject(one).get("_id").getAsString();
		if(""==_id||"".equals(_id))return false;
		boolean result=ec.deleteOne(indexName, typeName, _id);
		return result;
	}
	public <T> T queryOne(T one) {
		//根据baseVO进行查询
		JsonObject engineJSON=ec.selectOne(indexName, typeName, null, AbstractVO.toJsonObject(one));
		//将来engineJSON转为BaseVO.class
		one=(T)AbstractVO.fromJsonObject(engineJSON, one.getClass());
		return one;
	}
	public JsonObject queryOne(String id) {
		initialEc();
		//根据baseVO进行查询
		JsonObject one=ec.selectOne(indexName, typeName, id, null);
		closeEc();
		return one;
	}
	public <T> List<T> listAll(Class<T> classOfT) {
		List<JsonObject> jsonList = ec.queryAll(indexName, typeName);
		List<T> engineList=new ArrayList<T>();
		for (int i = 0; i < jsonList.size(); i++) {
			T one=(T)AbstractVO.fromJsonObject(jsonList.get(i), classOfT);
			engineList.add(one);
		}
		return engineList;
	}
	public QueryRespVO termAgg(QueryBuilder queryBuilder,String ...columnName){
		SearchRequestBuilder request=ec.prepareSearch(indexName, typeName, queryBuilder);
		AbstractAggregationBuilder aggregation;
		for(int i=0;i<columnName.length;i++){
			aggregation=AggregationBuilders.terms(columnName[i]).field(columnName[i]);
			request.addAggregation(aggregation);
		}
		
		return queryByRequest(request);
	}
	public <T> boolean updateOne(T one) {
		AbstractVO baseVO=(AbstractVO)one;
		boolean result=ec.updateOne(indexName, typeName, baseVO.get_id(), AbstractVO.toJsonObject(one));
		return result;
	}
	public QueryRespVO queryByRequest(SearchRequestBuilder request){
		return ec.executeQueryRequest(request);
	}
	public long getTotal() {
		long result=ec.getTotal(indexName, typeName);
		return result;
	}
	
}
